
***********************************************
* Rwanda provider study
***********************************************

***********************************************
* Contents
***********************************************

	* (0.0) Set up
	* (0.1) Variable definitions, saves $d_interm\fac_controls.dta 
	* 		Summary statistics
	* (0.2) Balance t-tests
		
***********************************************
* (0.0) Set up
***********************************************
		
	clear matrix
	clear all
	set mem 250m
	set matsize 1000
	set more off
	tempfile temp1
	* set scheme lean1, permanently
	pause on
    eststo clear

	cd "C:\Users\Diana\Dropbox\RWA_P4P\SPA\do_files"
	* Define file globals
	do rwanda_global_define.do
	
	cap log close

***********************************************
* (0.1) Variable definitions (facility covariates) 
***********************************************

	use "$f_inventory", clear

	* Merge geography
	merge 1:1 facil using "$r_geogr"
	
	* Create separate category for hospitals
	replace rollout=3 if factype==1	
	
	lab def rollout2 0 "Unaffected" 1 "Treatment" 2 "Control" 3 "Hospital"
	lab val rollout rollout2
		
	gen catchment=q113
	mvdecode catchment, mv(9999995=.m \ 9999998=.d)
	lab var catchment "Catchment population"
		
	* Use size of catchment are to normalize spending
	gen spendpc = ( q144d / catchment )
	la var spendpc "Spending/catchment population"
	gen logcatch=ln(catchment)
	lab var logcatch "Log catchment population"
	gen logspendpc=ln(spendpc)
	lab var logspendpc "Log per capita spending"
	
	* Generate new treatment variable so control is base
	gen treatment=(rollout==1)
	replace treatment=. if factype==1 /* hospitals unclassified */
	replace treatment=2 if rollout==0
	la def treatment 0 "Control" 1 "Treatment" 2 "Phase 0"
	la val treatment treatment 

	gen private=(mgauth==2)*100
	lab var private "Private/NGO/community (x100)"

	gen health_post=(factype==3)*100
	lab var health_post "Health post (x100)"
	
	recode adjac (2=0)
	lab var adjac "Adjacent to facility"
		
	***********************************************
	* Facility infrastructure
	***********************************************

	preserve
		keep facil uunit*
		reshape long uunit_, i(facil) j(unit_num) str
		gen type_genout=(uunit==1)
		gen type_anc=(uunit==3)
		gen type_tb=(uunit==6|uunit==28)
		gen type_hiv=(uunit==9|uunit==10|uunit==26|uunit==7)
		gen type_inmed=(uunit==22)
		
		foreach utype in genout anc tb hiv inmed {
			bysort facil: egen has_`utype'=total(type_`utype')
			recode has_`utype' (1/max=1)
			replace has_`utype'=has_`utype'*100
		}
		
		bysort facil: keep if _n==1
		
		lab var has_genout "Has general outpatient clinic (x100)"
		lab var has_anc "Has ANC clinic (x100)"
		lab var has_tb "Has inpatient/outpatient TB clinic (x100)"
		lab var has_hiv "Has VCT/HIV/special diagnoses (incl HIV) clinic (x100)"
		lab var has_inmed "Has inpatient medical clinic (x100)"
		
		keep facil has*
		save `temp1', replace
	restore
	
	merge 1:1 facil using `temp1', nogen
	
	gen beds_night=q116a
	replace beds_night=0 if q115==2 /* No overnight beds */
	lab var beds_night "Number of overnight beds"
	
	gen beds_inpat=q116b
	replace beds_inpat=0 if q114==2 | q116b==. /* No inpatient */
	lab var beds_inpat "Number of inpatient beds"
	
	gen ambulance=(q146==1)*100 if !mi(q146)
	lab var ambulance "Has functional ambulance (x100)"
	
	gen piped_wat=(q155==1)*100 if !mi(q155)
	lab var piped_wat "Water piped into facility (x100)"
	
	gen phone_radio=inlist(q159,1,2,3,4)*100 if !mi(q159)
	lab var phone_radio "Has phone or short-wave radio (x100)"
	
	gen comp=(q161==1)*100 if !mi(q161)
	lab var comp "Has functioning computer (x100)"
	
	qui eststo balance: estpost tabstat catchment private  /*
		*/ health_post has_genout has_anc has_tb has_hiv has_inmed /*
		*/ beds_night beds_inpat ambulance piped_wat phone_radio /*
		*/ comp adjac,  /*
		*/ by(rollout) stat(mean sd count) c(stat) 

	esttab balance using "$d_out/balance.tex", replace style(tex) booktabs   /*
		*/  cells( mean(fmt(%12.0fc)) sd( par(( )) )  count( par([ ]) )  ) unstack		 			/*
		*/  label  substitute( $tabsub ) $tabopts 			/*
		*/  noobs nonotes nonumbers collabels(, none) mlabels( ,none) /*
		*/  title("\label{tab:balance}Balance table, basic infrastructure") /*
		*/ 	note("")

	***********************************************
	* Finances and user fees
	***********************************************
	
	gen funds_equity   = strpos( q144, "A")!=0	if q144!=""
	gen funds_employer = strpos( q144, "B")!=0  if q144!=""
	gen funds_ins      = strpos( q144, "C")!=0  if q144!=""
	gen funds_pool     = strpos( q144, "D")!=0  if q144!=""
	gen funds_other    = strpos( q144, "X")!=0  if q144!=""
	gen funds_no	   = strpos( q144, "Y")!=0  if q144!=""
	gen funds_dk 	   = strpos( q144, "Z")!=0  if q144!=""
	
	la var funds_equity  	"Funding: equity fund for poor"
    la var funds_employer	"Funding: employer"
    la var funds_ins     	"Funding: insurance"
    la var funds_pool    	"Funding: gov't risk pool (various)"
    la var funds_other   	"Funding: other"
    la var funds_no	  		"Funding: No funding except user fees"
    la var funds_dk 	  	"Funding: Don't know"

	recode funds_equity funds_employer funds_ins funds_pool funds_other funds_no funds_dk (1=100)
	
	gen feesany = ( q141==1 ) if !mi( q141 )
	la  var feesany "Any routine user fees"

	mvdecode q142_?, mv( 8=.d )
	
	forvalues i = 1/8 {
		gen fees_`i' = ( q142_`i'==1) if !mi(q142_`i')
	}

	la var fees_1	"Client card"
	la var fees_2	"Consultation"
	la var fees_3	"Fee varies by diagnosis"
	la var fees_4	"Medications"
	la var fees_5	"Tests"
	la var fees_6	"Registration"
	la var fees_7	"Discounts/exemptions"
	la var fees_8	"Pre-pay for multiple visits"
	
	gen feesposted = inlist( q143, 1, 2) if !mi( q143 )
	la var feesposted "All/some fees posted"
	
	recode feesany fees_? feesposted (1=100)

	gen gov_disburse=q144a
	mvdecode gov_disburse, mv(999999999=.m \ 999999998=.d)
	replace gov_disburse=gov_disburse/catchment
	lab var gov_disburse "Gov reimburse for pooled risk/catchment (since Jan)"
	
	gen gov_client=q144c
	lab var gov_client "\# client visits linked with gov disbursements"
	
	qui eststo fees: estpost tabstat spendpc gov_disburse gov_client /*
		*/ funds_equity funds_employer funds_ins funds_pool funds_other /* 
		*/ funds_no funds_dk   feesany fees_? feesposted, /*
		*/ by(rollout) stat(mean count) c(stat)
	
	esttab fees using "$d_out/userfees.tex", replace style(tex) booktabs   /*
		*/  cells( mean(fmt(%12.0fc)) count( par([ ]) ) ) unstack		 			/*
		*/  label  substitute( $tabsub ) $tabopts 			/*
		*/  noobs nonotes nonumbers collabels(, none) mlabels( ,none) /*
		*/  varlabels( , blist(funds_equity "\addlinespace \multicolumn{@span}{l}{\emph{Funding sources other than routine budget or user fees$\dag$}}\\"   feesany "\addlinespace \\"  fees_1 "\addlinespace \multicolumn{@span}{l}{\emph{Specific user fees (if any routine fees)$\dag$}}\\" ) ) /*
		*/  title("\label{tab:userfees}Finances and user fees at facilities")	/*
		*/	note("$\dag$ Percent reporting affirmative.  Sample size in parenthesis.  Multiple answers possible for funding; gov't risk pool includes rama, farg and mmi.  Data from 42 hospitals in the 2007 Rwanda SPA are not available due confidentiality concerns.")

	eststo clear

	***********************************************
	* Saving facility covariates
	***********************************************

	keep facil catchment spendpc gov_disburse gov_client beds_night /*
		*/ beds_inpat private health_post has_genout has_anc has_tb /* 
		*/ has_hiv has_inmed ambulance piped_wat phone_radio comp /* 
		*/ rollout treatment factype funds* province adjac
		
	gen logcatch=ln(catchment)
	lab var logcatch "Log catchment population"
	gen logspendpc=ln(spendpc)
	lab var logspendpc "Log per capita spending"

	*** Rescale dichotomous variables into 0,1
	foreach var in private health_post has_genout has_anc /*
		*/ has_tb has_hiv has_inmed {
		replace `var'=`var'/100		
	}
 
	lab var has_genout "Clinic availability: general outpatient"
	lab var has_anc "Clinic availability: antenatal care"
	lab var has_tb "Clinic availability: inpatient/outpatient TB"
	lab var has_hiv "Clinic availability: VCT/HIV/special diagnoses"
	lab var has_inmed "Clinic availability: inpatient medical"

	foreach var in ambulance piped_wat phone_radio comp {
		replace `var'=`var'/100
	}
	
	lab var ambulance "Has functional ambulance"
	lab var piped_wat "Water piped into facility"
	lab var phone_radio "Has phone or short-wave radio"
	lab var comp "Has functioning computer"
	
	foreach var in funds_equity funds_employer funds_ins funds_pool funds_other funds_no {
		replace `var'=`var'/100
	}	

		
gen province_north= province==1
lab var province_north "Province: Northern" 
gen province_south= province==2
lab var province_south "Province: Southern" 
gen province_east= province==3
lab var province_east "Province: Eastern" 
gen province_west= province==4
lab var province_west "Province: Western" 
gen province_kig= province==5
lab var province_kig "Province: Kigali City" 

	save "$r_fac_controls", replace
	


**********************************
* (0.2) T-tests
**********************************
* Drop phase 0
drop if treatment==2

foreach var in private health_post adjac catchment spendpc {
	ttest `var', by(treatment)
	gen cmean_`var'=r(mu_1)
	gen diff_`var'=r(mu_2)-r(mu_1)
	gen p_`var'=r(p)
	gen t_`var'=r(t)
	gen type_`var'=0
	
}

foreach var in has_genout has_anc /*
	*/ has_tb has_hiv has_inmed {
	prtest `var', by(treatment)
	
	gen cmean_`var'=r(P_1)
	gen diff_`var'=r(P_2)-r(P_1)
	gen p_`var'=2*normprob(-abs(r(z)))
	gen t_`var'=r(z)
	gen type_`var'=2

	
}
 
hotelling has_genout has_anc has_tb has_hiv has_inmed, by(treatment)
gen ht_clinic=r(T2)


foreach var in funds_equity funds_employer funds_ins funds_pool funds_other /*
*/ funds_no{
	prtest `var', by(treatment)
	
	gen cmean_`var'=r(P_1)
	gen diff_`var'=r(P_2)-r(P_1)
	gen t_`var'=r(z)
	gen p_`var'=2*normprob(-abs(r(z)))
	gen type_`var'=4
}

 hotelling funds_equity funds_employer funds_ins funds_pool funds_other funds_no, by(treatment)
gen ht_funds=r(T2)


foreach var in province_north province_south province_east province_west province_kig {
	prtest `var', by(treatment)
	
	gen cmean_`var'=r(P_1)
	gen diff_`var'=r(P_2)-r(P_1)
	gen t_`var'=r(z)
	gen p_`var'=2*normprob(-abs(r(z)))
	gen type_`var'=6
}


hotelling province_north province_south province_east province_west province_kig, by(treatment)
gen ht_province=r(T2)

 keep diff_* p_* type_* cmean_* t_* ht_*
 keep if _n==1
 
 gen i_var=1
 
 reshape long diff_ p_ type_ cmean_ t_ ht_, i(i_var) j(var) string
 replace t_=-t_
 
 lab var var ""
 lab var cmean "Control mean"
 lab var diff "Difference"
 lab var p "P-value"
 lab var t_ "t-stat"
 lab var ht_ "Hotelling's t-squared"


replace var="Adjacent to main facility" if var=="adjac"
 replace var="Catchment population" if var=="catchment"

	
replace type=1 if var=="clinic"
 replace var="Clinic availability" if var=="clinic"
 replace var="General outpatient" if var=="has_genout"
 replace var="Antenatal care" if var=="has_anc"
 replace var="Inpatient/outpatient TB" if var=="has_tb"
 replace var="VCT/HIV/special diagnoses" if var=="has_hiv"
 replace var="Inpatient medical" if var=="has_inmed"
		
 
 replace var="Num client visits linked with gov disbursements" if var=="gov_client"
 replace var="Gov disbursements (for pooled risk) pc (since Jan)" if var=="gov_disburse"
 replace var="Per capita spending, 2006" if var=="spendpc"
 
 replace var="Health post" if var=="health_post"
 replace var="Private/NGO/community" if var=="private"
 replace var="Has functional ambulance" if var=="ambulance"
 replace var="Water piped into facility" if var=="piped_wat"
 replace var="Has phone or short-wave radio" if var=="phone_radio"
 replace var="Has functioning computer" if var=="comp" 
 
replace type=3 if var=="funds"
replace var="Funding source" if var=="funds"
 replace var="Equity fund for poor" if var=="funds_equity"
 replace var="Employer" if var=="funds_employer"
 replace var="Insurance" if var=="funds_ins"
 replace var="Government risk pool" if var=="funds_pool"
 replace var="Other" if var=="funds_other"
 replace var="User fees only" if var=="funds_no" 
 
replace type=5 if var=="province"
replace var="Province" if var=="province"
 replace var="Northern" if var=="province_north"
 replace var="Southern" if var=="province_south"
 replace var="Eastern" if var=="province_east"
 replace var="Western" if var=="province_west"
 replace var="Kigali City" if var=="province_kig"

 sort type_ var
 
tostring diff t_ cmean ht_, replace force format(%12.2f)
foreach var in diff t_ cmean ht_ {
	replace `var'="" if `var'=="."
}

** Add in p-values for hotelling
replace ht_="5.06 (p=0.42)" if var=="Clinic availability"
replace ht_="10.13 (p=0.13)" if var=="Funding source"
replace ht_="11.76 (p=0.02)" if var=="Province"

 texsave_dngo var cmean diff t_ ht_ using "$d_out/balance_diff.tex", replace /*
	*/ align(|p{7.5cm}rrrp{2cm}|) rowsep(7pt) /*
	*/ title(Facility characteristics: balance tests) /*
	*/ headlines(""\begin{center}" "My headline" "\end{center}"") frag /*
	*/ varlabels  underline("Clinic" "Province" "Funding")


use "$r_fac_controls", clear
gen public=private==0
lab var public "Public" 
lab var private "Private/NGO/community" 

gen control=treatment==0
lab var control "Control"
gen tr=treatment==1
lab var tr "Treatment"
gen pilot=treatment==2
lab var pilot "Pilot"

lab def ftype 1 "Hospital" 2 "Polyclinic" 3 "Health post"
lab val factype ftype

keep if rollout==1 | rollout==2

qui eststo summ_count: estpost tabstat public private /*
*/ province_north province_south province_east province_west province_kig /*
 */ has_genout has_inmed, by(factype) stat(sum) c(stat)
qui eststo summ_stat: estpost tabstat catchment spendpc, by(factype) stat(mean sd) c(stat)
	
esttab summ_count using "$d_out/summ_count.tex", replace style(tex) booktabs   /*
		*/  cells(sum(fmt(%12.0fc))) unstack				 			/*
		*/  label  						/*
		*/  noobs nonotes nonumbers collabels(, none) mlabels( ,none) /*
		*/  varlabels( , blist(public "\underline{Number of facilities} & & & \\ \emph{Management type} & & & \\"  has_genout "\emph{Clinic availability} & & & \\" province_north "\emph{Province} & & & \\" )) 

esttab summ_stat using "$d_out/summ_stat.tex", replace style(tex) booktabs   /*
		*/  cells(mean(fmt(%12.0fc)) sd(par([ ]))  ) unstack				 			/*
		*/  label align(|lrrr|) 						/*
		*/  noobs nonotes nonumbers collabels(, none) mlabels( ,none) /*
		*/  varlabels( , blist(catchment "\underline{Group means and standard deviations} & & & \\"  mgauth_public "\emph{Management authority} & & & \\"  providers "Interviews and clinical observations} & & & \\") ) /*
		*/  title("\label{tab:summ_stat}Sample summary statistics")	/*
		*/	addnotes("Data from hospitals excluded from remaining analysis due confidentiality concerns.Standard deviations in square brackets.")
